package utils 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.ContextMenuEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class CustomContextMenu 
	{
		private static var _instance:CustomContextMenu;
		private var _parent:DisplayObjectContainer;
		private var _debugWindowClass:Class;
		private var _debugWindow:DisplayObject;
		private const DEBUG_MENU_CAPTION:String = "== Debug ==";
		private const AUTHOR_MENU_CAPTION:String = "Дмитрий Гайдук";
		
		public function CustomContextMenu(lockClass:SingeltonLockMenu) 
		{
			if (_instance != null)
			{
				throw new Error("CustomContextMenu exist");
			}
		}
		
		public static function getInstance():CustomContextMenu
		{
			if (_instance == null)
			{
				_instance = new CustomContextMenu(new SingeltonLockMenu());
			}
			return _instance;
		}
		
		public function createMyMenu(parent:DisplayObjectContainer, debugClass:Class, 
										container:DisplayObjectContainer = null):void
		{
			_parent = container;
			_debugWindowClass = debugClass;
			
			if (_parent == null)
			{
				return;
			}
			
			var my_menu:ContextMenu = parent.contextMenu;
			if (my_menu == null) {
				my_menu = new ContextMenu();
			}
			//my_menu.hideBuiltInItems();
			var my_notice:ContextMenuItem = new ContextMenuItem(AUTHOR_MENU_CAPTION);
			my_notice.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, openLink);
			if (my_menu.customItems == null) {
				my_menu.customItems = [];
			}
			my_menu.customItems.push(my_notice);
			_parent.contextMenu = my_menu;
			my_menu.addEventListener(ContextMenuEvent.MENU_SELECT, handleMenuSelect);
		}
		
		private function handleMenuSelect(e:ContextMenuEvent):void 
		{
			if (ErrorsCollector.getInstance().errors.length > 0)
			{
				addDebugMenu();
			}
		}
		
		private function addDebugMenu():void
		{
			if (_parent == null)
			{
				return;
			}
			var my_menu:ContextMenu = _parent.contextMenu;
			for (var i:int = 0; i < my_menu.customItems.length; i++ )
			{
				var menuItem:ContextMenuItem = my_menu.customItems[i] as ContextMenuItem;
				if ((menuItem != null) && (menuItem.caption == DEBUG_MENU_CAPTION))
				{
					return;
				}
			}
			my_menu.hideBuiltInItems();
			var debugMenu:ContextMenuItem = new ContextMenuItem(DEBUG_MENU_CAPTION);
			debugMenu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, showDebugWindow);
			my_menu.customItems.push(debugMenu);
			_parent.contextMenu = my_menu;
		}
		
		private function openLink(e:ContextMenuEvent):void
		{
			navigateToURL(new URLRequest("http://colibriapps.blogspot.com/"));
		}
		
		private function showDebugWindow(e:ContextMenuEvent):void
		{
			if (_debugWindow == null && _debugWindowClass!=null)
			{
				_debugWindow = new _debugWindowClass() as DisplayObject;
			}
			
			if (_debugWindow != null)
			{
				_parent.addChild(_debugWindow);
			}
		}
		
	}

}
class SingeltonLockMenu
	{
		public function SingeltonLockMenu()
		{
			trace("Singelton menu Up");
		}
	}